package com.yq.interview.study.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName BlockingQueueDemo
 * @Description 阻塞队列（消息中间件的原理）
 * 生产者和消费者-》阻塞队列
 * <p>
 * 1、队列，
 * <p>
 * 2、阻塞队列
 * 优点：
 * <p>
 * 不得不阻塞：如何管理
 * @Author yaoqiang
 * @Date 2020/11/23 9:22
 * @Version 1.0
 * java集合类，
 * arrayList熟悉，用过arrayList和linkList，还有啥 CopyOnWriteList, BlockingQueue
 */
public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        //List list=new ArrayList();
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);//队列只有三个位置
        System.out.println(blockingQueue.offer("a",2L, TimeUnit.SECONDS));
        blockingQueue.offer("b",2L, TimeUnit.SECONDS);
        blockingQueue.offer("c",2L, TimeUnit.SECONDS);
        System.out.println("++++++++++++++");
   //     blockingQueue.take();
        System.out.println(blockingQueue.offer("d",2L, TimeUnit.SECONDS));

/*        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        System.out.println("++++++++++++++");
        blockingQueue.take();
        blockingQueue.put("d");*/
        //以下
     /*   System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d"));//插入成功true,如果多插入会返回false,
        System.out.println(blockingQueue.poll());//取头
*/
        //以下是会抛出异常的
/*        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        System.out.println(blockingQueue.add("d"));//IllegalStateException
        System.out.println(blockingQueue.remove());//NoSuchElementException
        System.out.println(blockingQueue.element());//获取头部元素*/


    }
}
